package com.chocho.producerserver.controller;

import com.chocho.producerserver.bean.QCollection;
import com.chocho.producerserver.bean.User;
import com.chocho.producerserver.service.QCollectionService;
import com.chocho.producerserver.utils.Msg;
import com.chocho.producerserver.vo.VCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.ElementType;
import java.util.List;
import java.util.Map;

/**
 * Created by chocho on 2022/4/10.
 */
@CrossOrigin
@RestController
@RequestMapping("/qcollection")
//@PreAuthorize("hasAuthority('collection')")
public class QCollectionController {
    private Logger logger = LoggerFactory.getLogger(QCollectionController.class);

    @Autowired
    private QCollectionService collectionService;

    @PreAuthorize("hasAuthority('collection:add')")
    @PostMapping("/add")
    @ResponseBody
    public Msg addCollection(HttpServletRequest request, @RequestBody VCollection collection){
        return collectionService.addCollection(request, collection) ? Msg.success() : Msg.fail();
    }

    @PreAuthorize("hasAuthority('collection:list')")
    @PostMapping("/select")
    @ResponseBody
    public Msg selectCollection(@RequestBody Map<String, Object> map){
        List<VCollection> collectionList = collectionService.getCollectionByQuery(map);
        return (collectionList != null)
                ? Msg.success().add("collectionList", collectionList)
                : Msg.fail();
    }

    @PreAuthorize("hasAuthority('collection:list')")
    @PostMapping("/select/my")
    @ResponseBody
    public Msg selectMyCollection(HttpServletRequest request, @RequestBody Map<String, Object> map){
        List<VCollection> collectionList = collectionService.getMyCollectionByQuery(request, map);
        return (collectionList != null)
                ? Msg.success().add("collectionList", collectionList)
                : Msg.fail();
    }

    @PreAuthorize("hasAuthority('collection:delete')")
    @GetMapping("/delete")
    @ResponseBody
    public Msg deleteCollection(String ids){
        if(ids != null && !"".equals(ids)){
            return collectionService.deleteCollection(ids) ? Msg.success() : Msg.fail();
        }
        return Msg.fail();
    }

    @PreAuthorize("hasAuthority('collection:update')")
    @PostMapping("/update")
    @ResponseBody
    public Msg updateCollection(HttpServletRequest request, @RequestBody VCollection collection){
        return collectionService.updateCollection(request, collection) ? Msg.success() : Msg.fail();
    }

    //根据ids查询题目集信息
    @PreAuthorize("hasAuthority('collection:get')")
    @GetMapping("/select/ids")
    @ResponseBody
    public Msg getCollectionByIdList(String ids){
        if(ids != null && !"".equals(ids)){
            List<VCollection> collectionList = collectionService.getCollectionByIdList(ids);
            return (collectionList != null && collectionList.size() > 0)
                    ? Msg.success().add("collectionList", collectionList)
                    : Msg.fail();
        }
        return Msg.fail();
    }

    //统计题目集下的所有题目的总分数
    @PreAuthorize("hasAuthority('collection:allscore')")
    @GetMapping("/count/all/score")
    @ResponseBody
    public Msg getAllScore(String collectionId){
        return Msg.success().add("allScore", collectionService.getAllScore(collectionId));
    }

    //计算题目集数量
    @PreAuthorize("hasAuthority('collection:count')")
    @GetMapping("/count")
    @ResponseBody
    public Msg countCollection(){
        return Msg.success().add("count", collectionService.count());
    }

    //统计题目集总做题人数
    @PreAuthorize("hasAuthority('collection:donum')")
    @GetMapping("/donum")
    @ResponseBody
    public Msg getDoNumById(String ids){
        return Msg.success().add("donumList", collectionService.getDoNum(ids));
    }
}
